<?php
namespace app\admin\controller;

use app\admin\logic\MachineVersionLogic;
use app\admin\validate\MachineVersionValidate;
use app\common\respond\HttpApi;

/**
 * 上位机版本类
 */
class MachineVersionController
{
    /**
     * 新增
     *
     * @param HttpApi $respond
     * @return \think\response\Json
     */
    public function add(HttpApi $respond)
    {
        $param = [
            'version_name' => request()->post('version_name'),
            'remark' => request()->post('remark')?: '',
            'file' => request()->file('file'),
        ];
        validate(MachineVersionValidate::class)->check($param);
        $logic = new MachineVersionLogic();
        $logic->add($param['version_name'], $param['remark'], $param['file']);

        return $respond->success([]);
    }

    /**
     * 列表
     *
     * @param HttpApi $respond
     * @return void
     */
    public function getList(HttpApi $respond)
    {
        $param = [
            'page' => (int)request()->get('page')?: 1,
            'page_size' => (int)request()->get('page_size')?: 10,
            'version_name' => request()->get('version_name'),
        ];
        $rule = [
            'page' => '>:0',
            'page_size' => '>:0',
        ];
        validate($rule)->check($param);

        $logic = new MachineVersionLogic();
        $data = $logic->getList($param);

        return $respond->success($data);
    }

    /**
     * 删除
     *
     * @param HttpApi $respond
     * @return void
     */
    public function del(HttpApi $respond)
    {
        $param = [
            'id' => (int)request()->post('id'),
        ];
        $rule = [
            'id' => '>:0',
        ];
        validate($rule)->check($param);

        $logic = new MachineVersionLogic();
        $logic->del($param['id']);

        return $respond->success([]);
    }

    /**
     * 关联机器
     *
     * @param HttpApi $respond
     * @return void
     */
    public function machine(HttpApi $respond)
    {
        $param = [
            'version_id' => (int)request()->post('version_id'),
            'force' => (int)request()->post('force'),
            'machine_ids' => request()->post('machine_ids'),
        ];
        $rule = [
            'version_id' => '>:0',
            'force' => 'in:0,1',
            'machine_ids' => 'require|array',
        ];
        validate($rule)->check($param);

        $logic = new MachineVersionLogic();
        $logic->machine($param['version_id'], $param['force'], $param['machine_ids']);

        return $respond->success([]);
    }
}
